export default {
    changeJson(item) {
        // 将对象格式化成字符串
        let str = JSON.stringify(item) // JSON.stringify()用于将JavaScript对象或值转换为JSON字符串
        let str_ = str.slice(1, str.length - 1)
        let arr = []
        for (let i = 0; i < str_.length; i++) {
            arr.push(str_[i].replace('"', ''))
        }
        for (let j = 0; j < arr.length; j++) {
            if (arr[j] === '') {
                arr.splice(j, 1)
            }
        }
        let res = arr.join('')
        res = res.replaceAll(":", "=")
        res = res.replaceAll(",", "&")
        return res;
    },

    /**
     * @description: 判断是否有按钮级权限1
     * @param {String} permission 多个使用 & 或 | 分隔开
     * @param {String} separator 分隔符：&-并且 |-或者
     * @return {*}
     * @author: gumingchen
     */
    havePermission(permission, separator = '&') {
        let result = false
        const permissions = permission
        let fn = ''
        switch (separator) {
            case '&':
                fn = 'every'
                break
            case '|':
                fn = 'some'
                break
        }
        const list = uni.getStorageSync('permission') || [];
        console.log(list, permission, "permission")
        result = fn && permissions[fn](item => {
            return list.indexOf(item) !== -1
        })
        return result
    },

    /**
     *  获取数据字典
   * dictValue,value
   * */
    selectDict(dictValue) {
        const list = uni.getStorageSync('dictionaries') ? uni.getStorageSync('dictionaries') : [];
        let datas = list
        if (dictValue) {
            datas = datas.filter(f => {
                return f.value == dictValue
            })
        }
        if (datas && datas.length > 0) {
            return datas[0].children
        }
        return [];
    },
    /**
     回显数据字典
    * dictValue,value
    * */
    selectDictLabel(dictValue, value) {
        const list = suni.getStorageSync('dictionaries') ? uni.getStorageSync('dictionaries') : [];
        dictValue += ""
        value += ""
        let datas = list
        if (!dictValue || !value) {
            return "";
        }
        if (dictValue) {
            datas = datas.filter(f => {
                return f.value == dictValue
            })
        }
        for (let data of datas) {
            if (data.children) {
                for (let children of data.children) {
                    if (children.value == value) {
                        return children.name
                    }
                }
            }
        }
    },
    /**
     * 获取时间
     * @param dateStamp 时间戳
     * */
    getDate(dateStamp) {
        //获取当前时间并打印
        let date = new Date();
        if (dateStamp) {
            date = new Date(dateStamp);
        }
        let yy = date.getFullYear();
        let mm = date.getMonth() + 1;
        let dd = date.getDate();
        let hh = date.getHours();
        let mf = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
        let ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        return yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
    },

    /**判断字符串是否为JSON格式*/
    isJSON(str) {
        if (typeof str == 'string') {
            try {
                var obj = JSON.parse(str);
                if (typeof obj == 'object' && obj) {
                    return true;
                } else {
                    return false;
                }

            } catch (e) {
                return false;
            }
        }
    },
    /**
     * 计算两个时间之间的时间差 多少天
     * */
    intervalTime(startTime, endTime) {
        // 计算两个日期之间的时间差（毫秒）
        const timeDiff = startTime.getTime() - endTime.getTime();

        // 将时间差转换为天数
        const daysDiff = Math.ceil(timeDiff / (1000 * 60 * 60 * 24));

        return daysDiff;
    },

    /**
     * 检验是否开启通知权限
     * */
    enablPermission(){
        //#ifdef APP-PLUS
        let main = plus.android.runtimeMainActivity();
        let pkName = main.getPackageName();
        let uid = main.getApplicationInfo().plusGetAttribute("uid");
        let NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
        //android.support.v4升级为androidx
        if (NotificationManagerCompat == null) {
            NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
        }
        let areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
        // 未开通‘允许通知'权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
        if (!areNotificationsEnabled) {
            uni.showModal({
                title: '通知权限开启提醒',
                content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
                showCancel: false,
                confirmText: '去设置',
                success: function(res) {
                    if (res.confirm) {
                        let Intent = plus.android.importClass('android.content.Intent');
                        let Build = plus.android.importClass("android.os.Build");
                        //android 8.0引导
                        if (Build.VERSION.SDK_INT >= 26) {
                            let intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
                            intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
                        } else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
                            let intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
                            intent.putExtra("app_package", pkName);
                            intent.putExtra("app_uid", uid);
                        } else { //(<21)其他--跳转到该应用管理的详情页
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            let uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
                            intent.setData(uri);
                        }
                        // 跳转到该应用的系统通知设置页
                        main.startActivity(intent);
                    }
                }
            });
        }
        //#endif
    },
    isEmpty(str){
        if (!str || str === "" || str === undefined || str === "undefined"){
            return true
        }
        return false
    },
    isNotEmpty(str){
        return !this.isEmpty(str)
    },
}